package com.codeyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codeyang.constant.AreaConstant;
import com.codeyang.domain.Area;
import com.codeyang.mapper.AreaMapper;
import com.codeyang.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * 描述: TO-删除标志-DO
 *
 * @author CodeYang_Site
 * @version 2021/5/26 13:53
 */
@CacheConfig(cacheNames = "com.codeyang.service.impl.AreaServiceImpl")
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements AreaService {


    @Autowired
    private AreaMapper areaMapper;


    /**
     * 区域管理:通过父id查询子
     * step:
     * 数据一般来自于国家地理局官网,在收集转为json|数据库记录对象
     * 一般不会发生更改|可以缓存
     *
     * @return
     */
    @Cacheable(key = AreaConstant.AREA_PREFIX)
    @Override
    public List<Area> list() {
        return super.list();
    }


    /**
     * 根据pid来查询 区域信息
     * step:
     * pid为0时默认查询省份信息
     * 1. 省 市 区(县)
     *
     * @param pId
     * @return
     */
    @Override
    public List<Area> listByPid(Long pId) {
        List<Area> areas = areaMapper.selectList(new LambdaQueryWrapper<Area>()
                .eq(Area::getParentId, pId)
        );

        if (CollectionUtils.isEmpty(areas)) {
            return Collections.emptyList();
        }
        return areas;
    }
}
